package org.divineryang.service.impl;

import org.checkerframework.checker.units.qual.A;
import org.divineryang.exception.NewBeeException;
import org.divineryang.mapper.SysMenuMapper;
import org.divineryang.mapper.SysRoleMenuMapper;
import org.divineryang.model.entity.system.SysMenu;
import org.divineryang.model.entity.system.SysUser;
import org.divineryang.model.vo.common.ResultCodeEnum;
import org.divineryang.model.vo.system.SysMenuVo;
import org.divineryang.service.SysMenuService;
import org.divineryang.utils.AuthContextUtil;
import org.divineryang.utils.MenuHelp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @program new-bee
 * @Description
 * @Author Diviner Yang
 * @Date 2024/04/21 18:05
 * @Version 1.0
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysMenu> findNodes() {
        //查询所有菜单
        List<SysMenu> menus=sysMenuMapper.findAll();
        if (CollectionUtils.isEmpty(menus)){
            return null;
        }
        List<SysMenu> tree = MenuHelp.buildTree(menus);
        //生成树形结构
        return tree;
    }

    @Override
    public void save(SysMenu sysMenu) {
        sysMenuMapper.save(sysMenu);
        // 新添加一个菜单，那么此时就需要将该菜单所对应的父级菜单设置为半开
        updateSysRoleMenuIsHalf(sysMenu) ;
    }

    private void updateSysRoleMenuIsHalf(SysMenu sysMenu) {
        // 查询是否存在父节点
        SysMenu parentMenu = sysMenuMapper.selectById(sysMenu.getParentId());
        if(parentMenu != null) {
            // 将该id的菜单设置为半开
            sysRoleMenuMapper.updateSysRoleMenuIsHalf(parentMenu.getId()) ;
            // 递归调用
            updateSysRoleMenuIsHalf(parentMenu) ;
        }
    }

    @Override
    public void updateById(SysMenu sysMenu) {
        sysMenuMapper.updateById(sysMenu);
    }

    @Override
    public void removeById(Long id) {
        int count = sysMenuMapper.countByParentId(id);  // 先查询是否存在子菜单，如果存在不允许进行删除
        if (count > 0) {
            throw new NewBeeException(ResultCodeEnum.NODE_ERROR);
        }
        sysMenuMapper.removeById(id);
    }

    @Override
    public List<SysMenuVo> findUserMenuList() {
        //查询所有菜单
        SysUser sysUser = AuthContextUtil.get();
        Long userId = sysUser.getId();
        List<SysMenu> sysMenuList = sysMenuMapper.selectListByUserId(userId) ;
        //封装成树形结构
        List<SysMenu> menus = MenuHelp.buildTree(sysMenuList);
        //返回vo
        List<SysMenuVo> menuVos=buildMenus(menus);
        return menuVos;
    }
    // 将List<SysMenu>对象转换成List<SysMenuVo>对象
    private List<SysMenuVo> buildMenus(List<SysMenu> menus) {
        List<SysMenuVo> sysMenuVoList = new LinkedList<SysMenuVo>();
        for (SysMenu sysMenu : menus) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }
}
